using System.Text;
using UnityEngine;

public class Log2File
{
    string filePath;
    StringBuilder sb = new StringBuilder(1024);
    public Log2File()
    {
        var date = System.DateTime.Now;
        filePath = $"GameLog_{date.Year}_{date.Month}_{date.Day}_{date.Hour}_{date.Minute}_{date.Second}.txt";
        Application.logMessageReceived += LogCallback;
    }

    public void OnDestroy()
    {
        Application.logMessageReceived -= LogCallback;
        System.IO.File.WriteAllText(filePath, sb.ToString());
        sb.Clear();
    }

    void LogCallback(string condition, string stackTrace, LogType type)
    {
        sb.Append($"[{type}]:[{condition}]\r\n");
        if (type != LogType.Log)
        {
            sb.Append($"[{type}]:[{stackTrace}]\r\n");
        }
    }
}